﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Threading;
using System.Xml;
using System.IO;
using Application.Common;
using Application.Presentation;
using Interface.Security;
using Interface.FormBuilder;

namespace IQCare.FormBuilder
{
    public partial class frmSCM_PharmacyMasterImportExport : Form
    {
        public frmSCM_PharmacyMasterImportExport()
        {
            InitializeComponent();
            lblmessage.Text = "";
        }

        

        private void btnexport_Click(object sender, EventArgs e)
        {
            string dir = @"C:\SCMExport";
            IImportExport theExportManager = (IImportExport)ObjectFactory.CreateInstance("BusinessProcess.FormBuilder.BImportExport, BusinessProcess.FormBuilder");
            DataSet theDS = theExportManager.ExportSCMIQCare();

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            else
            {
                string[] filePaths = Directory.GetFiles(@"c:\SCMExport\");
                foreach (string filePath in filePaths)
                    File.Delete(filePath);
            }
            theDS.WriteXml("C:\\SCMExport\\SCMMaster.xml", XmlWriteMode.WriteSchema);
            lblmessage.Text = "Exported file has saved in c:\\SCMExport\\SCMMaster.xml";
            
            
        }

        private void btnImport_Click(object sender, EventArgs e)
        {

            if (txtfile.Text != "")
            {
                lblmessage.Text = "";
                string myXMLfile = txtfile.Text;

                string strext = Path.GetExtension(myXMLfile);
                //if (strext != ".xml")
                //{
                //    IQCareWindowMsgBox.ShowWindow("BrowseFile", this);
                //    return;
                //}
                //if (myXMLfile.ToUpper() != "C:\\SCMEXPORT\\SCMMASTER.XML")
                //{
                //    IQCareWindowMsgBox.ShowWindow("Browswrongfile", this);
                //    return;
                //}
                DataSet ds = new DataSet();
                System.IO.FileStream fsReadXml = new System.IO.FileStream
                    (myXMLfile, System.IO.FileMode.Open);
                try
                {
                    ds.ReadXml(fsReadXml);

                    if (ds.Tables[0].Rows[0]["AppVer"].ToString() != GblIQCare.AppVersion || ((DateTime)ds.Tables[0].Rows[0]["RelDate"]).ToString("dd-MMM-yyyy") != GblIQCare.ReleaseDate)
                    {
                        IQCareWindowMsgBox.ShowWindow("ImportScmMasterCheckVersion", this);
                        return;

                    }
                    IImportExport theExportManager = (IImportExport)ObjectFactory.CreateInstance("BusinessProcess.FormBuilder.BImportExport, BusinessProcess.FormBuilder");
                    if (ds.Tables.Count == 20)
                    {

                        theExportManager.BulkInsert(ds.Tables[1], "Mst_Drug");
                        theExportManager.BulkInsert(ds.Tables[2], "Mst_Generic");
                        theExportManager.BulkInsert(ds.Tables[3], "Lnk_DrugGeneric");
                        theExportManager.BulkInsert(ds.Tables[4], "Mst_DrugType");
                        theExportManager.BulkInsert(ds.Tables[5], "Lnk_DrugTypeGeneric");

                        theExportManager.BulkInsert(ds.Tables[6], "mst_strength");
                        theExportManager.BulkInsert(ds.Tables[7], "mst_frequency");
                        theExportManager.BulkInsert(ds.Tables[8], "lnk_drugstrength");
                        theExportManager.BulkInsert(ds.Tables[9], "lnk_drugfrequency");

                        theExportManager.BulkInsert(ds.Tables[10], "mst_dispensingunit");
                        theExportManager.BulkInsert(ds.Tables[11], "mst_manufacturer");
                        //theExportManager.BulkInsert(ds.Tables[12], "mst_batch");
                        //theExportManager.BulkInsert(ds.Tables[13], "mst_store");
                        theExportManager.BulkInsert(ds.Tables[14], "mst_supplier");

                        //theExportManager.BulkInsert(ds.Tables[15], "lnk_storesourcedestination");
                        theExportManager.BulkInsert(ds.Tables[16], "lnk_supplieritem");
                        //theExportManager.BulkInsert(ds.Tables[17], "lnk_storeitem");
                        theExportManager.ExportItemDrugType(ds.Tables[18]);
                        //theExportManager.BulkInsert(ds.Tables[19], "lnk_testparameter");

                        IQCareWindowMsgBox.ShowWindow("ImportSuccess", this);
                    }
                    else
                    {
                        IQCareWindowMsgBox.ShowWindow("Browsewrongfile", this);

                    }

                }

                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                finally
                {
                    fsReadXml.Close();
                }
            }
            else
            {
                IQCareWindowMsgBox.ShowWindow("BrowseFile", this);
                return;
            }
        }

        private void frmSCM_PharmacyMasterImportExport_Load(object sender, EventArgs e)
        {
            rdoImport.Checked = false;
            rdoExport.Checked = false;
            grpexport.Enabled = false;
            grpimport.Enabled = false;
            txtfile.Text = "";
        }

        private void rdoImport_CheckedChanged(object sender, EventArgs e)
        {
            if (rdoImport.Checked == true)
                grpimport.Enabled = true;
            else
                grpimport.Enabled = false;
        }

        private void rdoExport_CheckedChanged(object sender, EventArgs e)
        {
            if (rdoExport.Checked == true)
                grpexport.Enabled = true;
            else
                grpexport.Enabled = false;
        }

        private void btnBrowse_Click_1(object sender, EventArgs e)
        {
            openFileDialog1.DefaultExt = "*.xml";
            openFileDialog1.InitialDirectory = "c:\\SCMExport";
            openFileDialog1.ShowDialog();
            txtfile.Text = openFileDialog1.FileName; 
        }

        private void btnclose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
